﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Layout.Events;
using DevExpress.XtraGrid.Views.Layout;

namespace QLKS
{
    public partial class frmTinhTrangPhong : XtraForm
    {
        public frmTinhTrangPhong()
        {
            InitializeComponent();
        }

        private void layoutView1_CustomDrawCardCaption(object sender, DevExpress.XtraGrid.Views.Layout.Events.LayoutViewCustomDrawCardCaptionEventArgs e)
        {
            if (e.RowHandle > -1)
            {
                string cellValue = layoutView1.GetDataRow(e.RowHandle)["TinhTrangPhong"].ToString();
                if (cellValue == "1")
                    e.Appearance.ForeColor = Color.Red;
                else
                    e.Appearance.ForeColor = Color.Green;
            }
        }

        private void frmTinhTrangPhong_Load(object sender, EventArgs e)
        {
            gridControl1.DataSource = BUS.PhongBUS.SelectAll().Tables[0];
        }
        private void layoutView1_Click(object sender, EventArgs e)
        {
            Point p = MousePosition;
            p = gridControl1.PointToClient(p);
            DevExpress.XtraGrid.Views.Layout.ViewInfo.LayoutViewHitInfo hInfo = layoutView1.CalcHitInfo(p);
            if (IsMouseOverCardCaption(p))
            {
                if (layoutView1.GetDataRow(hInfo.RowHandle)["TinhTrangPhong"].ToString() != "1")
                {
                    if (!frmMain.sameForm("frmTaoPhieuThue"))
                    {
                        using (frmTaoPhieuThue pThue = new frmTaoPhieuThue())
                        {
                            pThue._phong.TenPhong = layoutView1.GetDataRow(hInfo.RowHandle)["TenPhong"].ToString();
                            pThue._phong.MaPhong = Convert.ToInt32(layoutView1.GetDataRow(hInfo.RowHandle)["MaPhong"]);
                            pThue.ShowDialog();
                        }
                    }
                }
                else
                {
                    using (frmLapHoaDon pThue = new frmLapHoaDon())
                    {
                        pThue._phong.MaPhong = Convert.ToInt32(layoutView1.GetDataRow(hInfo.RowHandle)["MaPhong"]);
                        pThue.ShowDialog();
                    }                    
                }
                gridControl1.DataSource = BUS.PhongBUS.SelectAll().Tables[0];
            }
        }
        private bool IsMouseOverCardCaption(Point p)
        {
            DevExpress.XtraGrid.Views.Layout.ViewInfo.LayoutViewHitInfo hInfo = layoutView1.CalcHitInfo(p);
            return hInfo.HitTest == DevExpress.XtraGrid.Views.Layout.ViewInfo.LayoutViewHitTest.CardCaption;
        }

        private void layoutView1_MouseMove(object sender, MouseEventArgs e)
        {
            if (IsMouseOverCardCaption(new Point(e.X, e.Y)))
                gridControl1.Cursor = Cursors.Hand;
            else
                gridControl1.Cursor = Cursors.Default;
        }
        private void layoutView1_CustomCardLayout(object sender, LayoutViewCustomCardLayoutEventArgs e)
        {
            string colGhiChu = layoutView1.Columns["GhiChu"].LayoutViewField.Name;            
            e.CardDifferences.AddItemDifference("Group1",LayoutItemDifferenceType.ItemVisibility, (layoutView1.GetDataRow(e.RowHandle)["KhachHang"].ToString()!=string.Empty));
            e.CardDifferences.AddItemDifference(colGhiChu, LayoutItemDifferenceType.ItemVisibility, (layoutView1.GetDataRow(e.RowHandle)["GhiChu"].ToString() != string.Empty));            
        }
    }
}